<template>
  <view class="content">
    <view class="blocks">
      <view class="block-box" v-for="block in blocks" :key="block.id">
        <view class="block-name">{{ block.name }}</view>
        <view class="block-items">
          <view class="item" v-for="item in block.funcs" :key="item.id" @click="toPage(item)">
            <view class="icon">
              <image :src="item.icon" mode="aspectFit" />
            </view>
            <view class="name">{{ item.name }}</view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
  import { ref } from 'vue';
  import { TOKEN_KEY } from '@/configs'
  import { getStorageSync, setStorageSync } from '@/utils/store';
  import { callCloudFunction } from '@/utils/cloudfunc';
	
  const blocks = ref([
    {
      id: 1,
      name: '生活服务',
      funcs: [
        { id: '1-1', name: '生活账本', icon: '/static/icons/account.png', page: '/pages/account/index'},
      ]
    },
    {
      id: 2,
      name: '超级大脑',
      funcs: [
        { id: '2-1', name: '礼尚往来', icon: '/static/icons/gift_lswl.png', page: '/pages/giftContact/index'},
        { id: '2-2', name: '密码簿', icon: '/static/icons/psd_book.png', page: '/pages/pwdBook/index'},
      ]
    }
  ])
  
  // 检查是否登录
  const isUserLogged = async () => {
    const token = getStorageSync(TOKEN_KEY)
    console.log('检查登录token:', token);
    if (token) {
      uni.showLoading({
        title:'跳转中'
      })
      const res = await callCloudFunction({
        name: 'ume-user',
        data: {action: 'tokenLogin'}
      })
      uni.hideLoading()
      console.log('isLogged返回:', res)
      if (res.token_update) {
        setStorageSync(TOKEN_KEY, res.token)
      }
      return res.token ? true : false
    } else {
      console.error('isLogged没有token')
      return false
    }
  }
      
  const toPage = async (p) => {
    console.log(p.page);
    if (!p.page) {
      uni.showModal({
        title: '提示',
        content: '程序员正在加班熬夜开发...'
      })
      return
    }
    // 是否登录，未登录就跳转登录页
    if (await isUserLogged()) {
      uni.navigateTo({
        url: p.page
      })
			// console.log('跳转', p.page);
    } else {
      console.log('false，走登录');
      uni.navigateTo({
        url: '/pages/user/login'
      })
    }
  }
</script>

<style lang="scss" scoped>
  .content {
  	background-color: #efefef;
  	padding: 10rpx;
    .blocks {
      .block-box {
        // width: 100%;
        margin-bottom: 10rpx;
        background-color: #fff;
        padding: 10rpx;
        border-radius: 10rpx;
        .block-name {
          font-size: 28rpx;
          color: #888;
        }
        .block-items {
          display: flex;
          .item {
            width: 140rpx;
            height: 140rpx;
            // border: 1px solid #eee;
            padding: 10rpx;
            text-align: center;
            .icon {
              width: 100%rpx;
              height: 90rpx;
              image {
                width: 90rpx;
                height: 90rpx;
              }
            }
            .name {
              font-size: 28rpx;
              line-height: 28rpx;
            }
          }
        }
      }
      
    }
  }
</style>